'use strict';

const execa = require('execa');
const { join } = require('path');
const { readdirSync } = require('fs');

function example(arr, name) {
  arr.forEach((packageName) => {
    const args = [];
    if (name) {
      if (name !== packageName) {
        return;
      }
      args.push(join(__dirname, '..', 'examples', name));
    } else {
      args.push(join(__dirname, '..', 'examples', packageName));
    }
    const { stdout, stderr } = execa('node', args);
    stdout.pipe(process.stdout);
    stderr.pipe(process.stderr);
  });
}

const [, , name] = process.argv;
const examples = join(__dirname, '..', 'examples');

example(readdirSync(examples), name);
